# python内置
import json
import xml.etree.ElementTree as ET
import ast
# 第三方库
# 自定义库
from configs.tools_configs import HANDLE_OPERATE_TYPES
from common.utils.message import fun_info


class FormatContent:
    def format(cls,format_type:str, content:str)->bool:
        if format_type in HANDLE_OPERATE_TYPES:
            format_flag,r_content = eval('cls.format_'+format_type)(content)
            if format_flag:
                return fun_info(format_flag,r_content)
            return fun_info(False,f'{format_type} is illegal !')
        else:
            return fun_info(False,'not support unknown format type !')
    
    def format_JSON(cls,content):
        try:
            content = ast.literal_eval(content)
            gui_content = json.dumps(content, indent=4,separators=(',', ':'))
        except ValueError:
            return False,None
        return True,gui_content


if __name__ == '__main__':
    HANDLE_OPERATE_TYPES = ['JSON','CSS','XML']
    FormatContent().format('JSON',str({'a':1}))